package org.hentai.bt.util.field;


import org.hentai.bt.anno.field.base.FieldBase;
import org.hentai.bt.anno.field.data.*;

import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.Map;

public class FieldAnnotationEntryFactory {

    public final static String DEFAULT = "org.hentai.bt.anno.field.base.FieldBase";

    public final static String TINYINT = "org.hentai.bt.anno.field.data.FieldTINYINT";
    public final static String SMALLINT = "org.hentai.bt.anno.field.data.FieldSMALLINT";
    public final static String INTEGER = "org.hentai.bt.anno.field.data.FieldINTEGER";
    public final static String BIGINT = "org.hentai.bt.anno.field.data.FieldBIGINT";

    public final static String FLOAT = "org.hentai.bt.anno.field.data.FieldFLOAT";
    public final static String DOUBLE = "org.hentai.bt.anno.field.data.FieldDOUBLE";

    public final static String VARCHER = "org.hentai.bt.anno.field.data.FieldVARCHER";

    public final static String TEXT = "org.hentai.bt.anno.field.data.FieldTEXT";

    public static FieldEntry getFieldEntry(Annotation annotation) {
        switch (annotation.annotationType().getName()) {
            case TINYINT:
                FieldTINYINT fieldTINYINT = (FieldTINYINT) annotation;

                return new FieldEntry(
                        fieldTINYINT.isNULL(), fieldTINYINT.defaultValue()
                );
            case SMALLINT:
                FieldSMALLINT fieldSMALLINT = (FieldSMALLINT) annotation;

                return new FieldEntry(
                        fieldSMALLINT.isNULL(), fieldSMALLINT.defaultValue()
                );
            case INTEGER:
                FieldINTEGER fieldINTEGER = (FieldINTEGER) annotation;

                return new FieldEntry(fieldINTEGER.isNULL(), fieldINTEGER.defaultValue());

            case BIGINT:
                FieldBIGINT fieldBIGINT = (FieldBIGINT) annotation;

                return new FieldEntry(fieldBIGINT.isNULL(), fieldBIGINT.defaultValue());
            case FLOAT:
                FieldFLOAT fieldFLOAT = (FieldFLOAT) annotation;

                return new FieldEntry(fieldFLOAT.isNULL(), fieldFLOAT.defaultValue());
            case DOUBLE:
                FieldDOUBLE fieldDOUBLE = (FieldDOUBLE) annotation;

                return new FieldEntry(fieldDOUBLE.isNULL(), fieldDOUBLE.defaultValue());
            case VARCHER:
                FieldVARCHER fieldVARCHER = (FieldVARCHER) annotation;

                return new FieldEntry(fieldVARCHER.isNULL(), fieldVARCHER.defaultValue(),
                        new HashMap<>() {{
                            put("maxlength", Integer.toString(fieldVARCHER.maxLength()));
                        }}
                );

            case TEXT:
                FieldTEXT fieldTEXT = (FieldTEXT) annotation;

                return new FieldEntry(fieldTEXT.isNULL(), fieldTEXT.defaultValue());
            case DEFAULT:
                FieldBase fieldBase = (FieldBase) annotation;
                return new FieldEntry(fieldBase.isNULL(), fieldBase.defaultValue());

            default:
                throw new IllegalStateException("Unexpected value: " + annotation.annotationType().getName());
        }

    }


    public static class FieldEntry {

        private String isNull;

        private String defaultValue;

        private Map<String, String> others;

        private FieldEntry() {
        }

        private FieldEntry(boolean isNull, Object defaultValue) {
            this.isNull = Boolean.toString(isNull);
            this.defaultValue = defaultValue.toString();
        }

        private FieldEntry(boolean isNull, Object defaultValue, Map<String, String> others) {
            this.isNull = Boolean.toString(isNull);
            this.defaultValue = defaultValue.toString();
            this.others = others;
        }

        public String getDefaultValue() {
            return defaultValue;
        }

        public String isNull() {
            return isNull;
        }

        public Map<String, String> getOthers() {
            return others;
        }

        @Override
        public String toString() {
            return "FieldEntry{" +
                    "isNull=" + isNull +
                    ", defaultValue=" + defaultValue +
                    ", others=" + others +
                    '}';
        }
    }

}
